﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.EntityClient;
using CongregacaoOs.Data;

namespace CongregacaoOs.Business
{
    [System.ComponentModel.DataObject]
    public class ccAdmBus : IDisposable
    {
        public void Dispose() { }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Insert, true)]
        public int AdmSaveOrUpdate(ccAdm adm)
        {
            if (adm.nome == "") { throw new Exception("O Nome não foi informado"); }
            if (adm.idRegional == 0) { throw new Exception("O Região não foi informado"); }

            using (EntityConnection conn = new EntityConnection("name = CcbOrdemServico"))
            {
                try
                {
                    using (EntityCommand cmd = conn.CreateCommand())
                    {
                        cmd.CommandText = "CcbOrdemServico.ccAdmSave";
                        cmd.CommandType = System.Data.CommandType.StoredProcedure;

                        EntityParameter param;

                        param = cmd.Parameters.Add("IDADM", DbType.Int32);
                        param.Direction = ParameterDirection.Input;
                        if (adm.idAdm != 0)
                            param.Value = adm.idAdm;
                        else
                            param.Value = DBNull.Value;

                        param = cmd.Parameters.Add("NOME", DbType.String);
                        param.Direction = ParameterDirection.Input;
                        if (adm.nome != null)
                            param.Value = adm.nome;
                        else
                            param.Value = DBNull.Value;


                        param = cmd.Parameters.Add("IDREGIONAL", DbType.Int32);
                        param.Direction = ParameterDirection.Input;
                        if (adm.idRegional != 0)
                            param.Value = adm.idRegional;
                        else
                            param.Value = DBNull.Value;

                        param = cmd.Parameters.Add("IDDISTRITAL", DbType.Int32);
                        param.Direction = ParameterDirection.Input;
                        if (adm.idDistrital != 0)
                            param.Value = adm.idDistrital;
                        else
                            param.Value = DBNull.Value;


                        param = cmd.Parameters.Add("ID", DbType.String);
                        param.Direction = ParameterDirection.Output;

                        using (conn)
                        {
                            if(conn.State == ConnectionState.Closed) conn.Open();
                            cmd.ExecuteNonQuery();
                        }
                        if (cmd.Parameters["ID"].Value != DBNull.Value)
                        {return cmd.Parameters["ID"].Value != null ? Convert.ToInt32(cmd.Parameters["ID"].Value) : 0;}
                        else return 0;
                    }
                }
                catch (Exception error)
                {
                    throw new Exception("Erro:" + error.Message);
                    return 0;
                }

            }
        }


        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
        public List<ccAdmSelect_Result> AdmSelect(ccAdm adm)
        {
            using (EntityConnection conn = new EntityConnection("name = CcbOrdemServico"))
            {                
                try
                {
                    using (EntityCommand cmd = conn.CreateCommand())
                    {
                        cmd.CommandText = "CcbOrdemServico.ccAdmSelect1";
                        cmd.CommandType = System.Data.CommandType.StoredProcedure;

                        EntityParameter param;

                        param = cmd.Parameters.Add("IDADM", DbType.Int32);
                        param.Direction = ParameterDirection.Input;
                        if (adm.idAdm != 0)
                            param.Value = adm.idAdm;
                        else
                            param.Value = DBNull.Value;

                        param = cmd.Parameters.Add("NOME", DbType.String);
                        param.Direction = ParameterDirection.Input;
                        if (adm.nome != null)
                            param.Value = adm.nome;
                        else
                            param.Value = DBNull.Value;

                        param = cmd.Parameters.Add("IDREGIONAL", DbType.Int32);
                        param.Direction = ParameterDirection.Input;
                        if (adm.idRegional != 0)
                            param.Value = adm.idRegional;
                        else
                            param.Value = DBNull.Value;

                        param = cmd.Parameters.Add("IDDISTRITAL", DbType.Int32);
                        param.Direction = ParameterDirection.Input;
                        if (adm.idDistrital != 0)
                            param.Value = adm.idDistrital;
                        else
                            param.Value = DBNull.Value;


                        
                        using (conn)
                        {
                            if(conn.State == ConnectionState.Closed) conn.Open();
                            using (EntityDataReader reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess))
                            {

                                List<ccAdmSelect_Result> _result = new List<ccAdmSelect_Result>();
                                while (reader.Read())
                                {
                                    ccAdmSelect_Result _adm = new ccAdmSelect_Result();
                                    _adm.idAdm = reader.IsDBNull(reader.GetOrdinal("idAdm")) == false ?  reader.GetInt32(reader.GetOrdinal("idAdm")) : 0;
                                    _adm.Adm = reader.IsDBNull(reader.GetOrdinal("Adm")) == false ? reader.GetString(reader.GetOrdinal("Adm")) : "";
                                    _adm.Regional = reader.IsDBNull(reader.GetOrdinal("regional")) == false ? reader.GetString(reader.GetOrdinal("regional")) : "";
                                    _adm.Distrital = reader.IsDBNull(reader.GetOrdinal("Distrital")) == false ? reader.GetString(reader.GetOrdinal("Distrital")) : "";
                                    _result.Add(_adm);
                                }                               
                                return _result;
                            }
                        }


                    }
                }
                catch (Exception)
                {
                    return null;
                }

            }
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Delete, true)]
        public void AdmDelete(int id)
        {
            try
            {
                using (CcbOrdemServico objDB = new CcbOrdemServico())
                {
                    objDB.ccAdmDelete(id);
                }
            }
            catch (System.Exception)
            {

                throw;
            }
        }
    }
}
